<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 数字反转打印（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>小华是个对数字很敏感的小朋友&#xff0c;他觉得数字的不同排列方式有特殊美感。</p> 
<p>某天&#xff0c;小华突发奇想&#xff0c;如果数字多行排列&#xff0c;第一行1个数&#xff0c;第二行2个&#xff0c;第三行3个&#xff0c;即第n行有n个数字&#xff0c;并且奇数行正序排列&#xff0c;偶数行逆序排列&#xff0c;数字依次累加。</p> 
<p>这样排列的数字一定很有意思。聪明的你能编写代码帮助小华完成这个想法吗&#xff1f;</p> 
<p>规则总结如下&#xff1a;</p> 
<p>a、每个数字占据4个位置&#xff0c;不足四位用‘*’补位&#xff0c;如1打印为1***。<br /> b、数字之间相邻4空格。<br /> c、数字的打印顺序按照正序逆序交替打印,奇数行正序&#xff0c;偶数行逆序。<br /> d、最后一行数字顶格&#xff0c;第n-1行相对第n行缩进四个空格</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行输入为N&#xff0c;表示打印多少行; 1&lt;&#61;N&lt;&#61;30</p> 
<p>输入&#xff1a;2</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>xxxx1***</p> 
<p>3***xxxx2***</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">2</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;"> <p><code>   1***</code></p> <p><code>3***    2***</code></p> </td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">符号*表示&#xff0c;数字不满4位时的补位&#xff0c;符号X表示数字之间的空格。注意实际编码时不需要打印X&#xff0c;直接打印空格即可。此处为说明题意&#xff0c;故此加上X。</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>这题没啥好说的&#xff0c;按照要求写就行&#xff0c;就是感觉小华有点欠揍。</p> 
<p></p> 
<p>贴一下N&#61;30的运行效果图</p> 
<p><img alt="" height="566" src="https://img-blog.csdnimg.cn/8a457dfdd33f4312ac61614c180cf4ef.png" width="1200" /></p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;
import java.util.StringJoiner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    int n &#61; sc.nextInt();
    getResult(n);
  }

  public static void getResult(int n) {
    // 每行要打印的数&#xff0c;起始为第一行第一个数1
    int print &#61; 1;

    for (int i &#61; 1; i &lt;&#61; n; i&#43;&#43;) { // 多少行
      int[] printList &#61; new int[i];
      for (int j &#61; 0; j &lt; i; j&#43;&#43;) { // 每行多少个数
        printList[j] &#61; print&#43;&#43;;
      }

      if (i % 2 &#61;&#61; 0) reverse(printList); // 如果是偶数行&#xff0c;则逆序

      StringJoiner printListStr &#61; new StringJoiner(&#34;    &#34;);
      for (int ele : printList) {
        StringBuilder sb &#61; new StringBuilder(ele &#43; &#34;&#34;);
        while (sb.length() &lt; 4) sb.append(&#34;*&#34;); // 如果数字不足4位则后面用*补足
        printListStr.add(sb);
      }

      StringBuilder res &#61; new StringBuilder(printListStr.toString());
      for (int k &#61; 0; k &lt; n - i; k&#43;&#43;) {
        res.insert(0, &#34;    &#34;); // 每行的缩进
      }
      System.out.println(res);
    }
  }

  public static void reverse(int[] printList) {
    int l &#61; 0;
    int r &#61; printList.length - 1;

    while (l &lt; r) {
      int tmp &#61; printList[l];
      printList[l] &#61; printList[r];
      printList[r] &#61; tmp;
      l&#43;&#43;;
      r--;
    }
  }
}
</code></pre> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const n &#61; parseInt(line);

  let print &#61; 1; // 每行要打印的数&#xff0c;起始为第一行第一个数1
  for (let i &#61; 1; i &lt;&#61; n; i&#43;&#43;) { // 多少行
    let printList &#61; [];
    for (let j &#61; 0; j &lt; i; j&#43;&#43;) { // 每行多少个数
      printList.push(print&#43;&#43;);
    }
    if (i % 2 &#61;&#61;&#61; 0) { // 如果是偶数行&#xff0c;则逆序
      printList.reverse();
    }
    printList &#61; printList.map((ele) &#61;&gt; { // 如果数字不足4位则后面用*补足
      let arr &#61; (ele &#43; &#34;&#34;).split(&#34;&#34;);

      while (arr.length &lt; 4) {
        arr.push(&#34;*&#34;);
      }

      return arr.join(&#34;&#34;);
    });

    let res &#61; [];
    res.push(printList.join(&#34;    &#34;));
    for (let m &#61; 0; m &lt; n - i; m&#43;&#43;) { // 每行的缩进
      res.unshift(&#34;    &#34;);
    }
    console.log(res.join(&#34;&#34;));
  }
});
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())


# 算法入口
def getResult():
    printNum &#61; 1  # 每行要打印的数&#xff0c;起始为第一行第一个数1
    for i in range(1, n &#43; 1):
        printList &#61; []
        for j in range(i):
            # 每行多少个数
            printList.append(printNum)
            printNum &#43;&#61; 1

        if i % 2 &#61;&#61; 0:
            # 如果是偶数行&#xff0c;则逆序
            printList.reverse()

        sArr &#61; []

        for ele in printList:
            tmp &#61; str(ele)

            # 如果数字不足4位则后面用*补足
            while len(tmp) &lt; 4:
                tmp &#43;&#61; &#34;*&#34;

            sArr.append(tmp)

        res &#61; &#34;    &#34;.join(sArr)

        for k in range(n - i):
            # 每行的缩进
            res &#61; &#34;    &#34; &#43; res

        print(res)


# 调用算法
getResult()
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>